const { Setting } = require("../db/index.cjs");
const { Op } = require('sequelize')
const { isEmpty } = require('lodash')

module.exports = [
	{
		channel: 'setting.getList',
		listener: (evt, key) => {
			const where = {};
			if (!isEmpty(key)) {
				where.key = {
					[Op.like]: `%${key}%`
				}
			}
			return Setting.findAll({
				where
			})
		}
	},
	{
		channel: 'setting.save',
		listener: (evt, data) => {
			if (Array.isArray(data)) {
				return Setting.bulkCreate(data, {
					updateOnDuplicate: ['value']
				})
			}
			return Setting.bulkCreate([data], {
				updateOnDuplicate: ['value']
			})
		}
	},
	{
		channel: 'setting.getByKey',
		listener: (evt, key) => Setting.findOne({ where: { key: { [Op.eq]: key } } })
	}
]
